﻿using System;
using System.Globalization;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Xml;
using System.Reflection;
using Services;
using Utilities.Menu.UIElements;
using BowMaster.Resources;
using Services.Menu;
using Utilities;
using BowMaster.SceneElements;

namespace BowMaster.SceneElements
{
    public class StaticObstacle
    {
        //public static int width = 40;
        //public static int height = 173;
        Texture2D texture;
        //protected bool invincible;
        public float life;
        protected float max_life;
        protected Rectangle drawRect;
        
        public StaticObstacle(Texture2D texture, int x, int y, int width, int height)
            : this(texture, new Rectangle(x, y - height-10, width, height))
        {
        }
        public StaticObstacle(Texture2D texture, Rectangle drawRect)
        {
            this.texture = texture;
            //this.invincible = true;
            this.life = this.max_life = 1;
            this.drawRect = drawRect;
        }
        public virtual void Draw(SpriteBatch spriteBatch, Matrix view, Matrix projection)
        {
            spriteBatch.DrawTexture(texture, drawRect, -1, Color.White, view, projection);
        }
        public virtual bool Hitted(Arrow a)
        {
            return StaticUtilities.RectContainsVect(drawRect, a.point);
        }
    }

    public class PersonObstacle : StaticObstacle
    {
        static int width = 60;
        static int height = 100;

        ArrayList<Arrow> hittingArrows;
        ArrayList<HitBox> hitBoxes;
        Rectangle lifeRect;
        Texture2D lifeText;
        public PersonObstacle(Texture2D texture, Texture2D lifeTexture, int x, int y)
            : base(texture, new Rectangle(x, y - height-10, width, height))
        {
            this.hittingArrows = new ArrayList<Arrow>();
            this.hitBoxes = new ArrayList<HitBox>();
            this.lifeText = lifeTexture;
            hitBoxes.Add(new HitBox()
            {
                box = new Rectangle(drawRect.X+ 8,drawRect.Y+ 1,28,36),
                value=0.5f
            });
            hitBoxes.Add(new HitBox()
            {
                box = new Rectangle(drawRect.X + 9, drawRect.Y + 36, 42, 37),
                value = 0.2f
            });
            hitBoxes.Add(new HitBox()
            {
                box = new Rectangle(drawRect.X + 2, drawRect.Y + 41, 7, 33),
                value = 0.2f
            });
            hitBoxes.Add(new HitBox()
            {
                box = new Rectangle(drawRect.X + 51, drawRect.Y + 52, 6, 19),
                value = 0.2f
            });
            hitBoxes.Add(new HitBox()
            {
                box = new Rectangle(drawRect.X + 8, drawRect.Y + 72, 46, 24),
                value = 0.1f
            });
            lifeRect = new Rectangle(drawRect.X, drawRect.Y - 10, drawRect.Width, 4);
        }

        public override bool Hitted(Arrow a)
        {
            bool hit = false;

            for (int i = 0; i < hitBoxes.Count; i++)
            {
                var h = hitBoxes[i];
                if (StaticUtilities.RectContainsVect(h.box, a.point))
                {
                    hit = true;
                    life -= max_life * h.value;
                    lifeRect.Width = (int)(drawRect.Width * life / max_life);
                    break;
                }
            }

            return hit;
        }
        public override void Draw(SpriteBatch spriteBatch, Matrix view, Matrix projection)
        {
            base.Draw(spriteBatch, view, projection);
            spriteBatch.DrawTexture(lifeText, lifeRect, Color.Red, view, projection);
        }
    }
}
